package 刷题.leedcode203;
//添加一个虚拟头节点
public class Solution203_3 {
    public ListNode removeElements(ListNode head,int val){
        //创建一个虚拟头节点
        ListNode dummyNode = new ListNode(val - 1);
        dummyNode.next = head;
        ListNode prev = dummyNode;
        //确保当前节点后还有节点
        while(prev.next != null){
            if(prev.next.val == val){
                prev.next = prev.next.next;
            }else{
                prev = prev.next;
            }
        }
        return dummyNode.next;
    }
}
